//引入上一级目录下的mysql连接池对象
const pool = require('../pool.js');
const express = require('express');
const jwt = require('jsonwebtoken')
const key = 'user key' //秘钥
//创建空路由器
var router = express.Router();
//添加路由
//1.用户注册
router.post('/register', (req, res, next) => {
	//获取post请求的数据
	var obj = req.body;
	//判断用户名是否为空
	var $uname = obj.uname;
	if (!$uname) {
		res.send({
			code: 401,
			msg: 'uname required'
		});
		//阻止继续往后执行
		return;
	}
	//验证密码、邮箱、手机是否为空
	var $upwd = obj.upwd;
	if (!$upwd) {
		res.send({
			code: 402,
			msg: 'upwd required'
		});
		return;
	}
	var $email = obj.email;
	if (!$email) {
		res.send({
			code: 403,
			msg: 'email required'
		});
		return;
	}
	var $phone = obj.phone;
	if (!$phone) {
		res.send({
			code: 404,
			msg: 'phone required'
		});
		return;
	}
	//执行SQL语句，将注册的数据插入到xh_user数据表中，成功响应 {code:200,msg:'register suc'}
	pool.query('INSERT INTO xh_user SET ?', [obj], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		//是否添加成功
		if (result.affectedRows > 0) {
			res.send({
				code: 200,
				msg: 'register suc'
			});
		}
	});
});
//2.用户登录路由
router.post('/login', (req, res, next) => {
	var obj = req.body;
	//验证数据是否为空
	var $uname = obj.uname;
	var $upwd = obj.upwd;
	if (!$uname) {
		res.send({
			code: 401,
			msg: 'uname required'
		});
		return;
	}
	if (!$upwd) {
		res.send({
			code: 402,
			msg: 'upwd required'
		});
		return;
	}
	//执行SQL语句，查看是否登录成功（使用用户名和密码两个条件能查询到数据）
	pool.query('SELECT * FROM xh_user WHERE uname=? AND upwd=?', [$uname, $upwd], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		// 登录成功
		if (result.length != 0) {
			req.session.loginUname = $uname;
			req.session.loginUid = result[0].xid;
			console.log(req.session);
			// 生成token
			const payload = {
				usexid: result[0].xid,
				uname: result[0].uname,
				avatar: result[0].avatar,
				user_name:result[0].user_name
			}
			res.send({
				code: 200,
				message: 'login success',
				payload
			})
		} else {
			// 登录失败
			res.send({
				code: 403,
				message: 'uname or upwd error!'
			})
		}
	});
});
//3.用户检索
router.get('/detail/:xid', (req, res, next) => {
	//获取get请求的数据
	var obj = req.params;
	//console.log(obj);
	//判断是否为空
	var $xid = obj.xid;
	if (!$xid) {
		res.send({
			code: 401,
			msg: 'xid required'
		});
		return;
	}
	//响应查询到的用户对象
	pool.query('SELECT * FROM xh_user WHERE xid=?', [$xid], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		//如何判断是否检索到了用户
		//判断结果（数组）长度是否大于0
		if (result.length > 0) {
			res.send({
				code: 200,
				msg: '检索成功',
				data: result
			});
		} else {
			res.send({
				code: 301,
				msg: 'detail err'
			});
		}
	});
})
//4.更改用户
//获取数据，验证是否为空
//执行SQL语句，修改用户表中对应的数据
router.put('/info', (req, res, next) => {
	var obj = req.body;
	var $xid = obj.xid;
	var $email = obj.email
	var $phone = obj.phone;
	var $sex = obj.sex;
	var $user_name = obj.user_name;
	var $avatar = obj.avatar;
	var $detail = obj.detail;
	if (!$xid) {
		res.send({
			code: 401,
			msg: 'xid required'
		});
		return;
	}
	if (!$email) {
		res.send({
			code: 402,
			msg: 'email required'
		});
		return;
	}
	if (!$phone) {
		res.send({
			code: 403,
			msg: 'phone required'
		});
		return;
	}
	if (!$sex) {
		res.send({
			code: 404,
			msg: 'sex required'
		});
		return;
	}
	if (!$user_name) {
		res.send({
			code: 405,
			msg: 'user_name required'
		});
		return;
	}
	if (!$avatar) {
		res.send({
			code: 406,
			msg: 'avatar required'
		});
		return;
	}
	if (!$detail) {
		res.send({
			code: 407,
			msg: 'detail required'
		});
		return;
	}
	//执行SQL语句
	pool.query('UPDATE xh_user SET? WHERE xid=?', [obj, obj.xid], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		//判断是否更改成功
		if (result.affectedRows > 0) {
			res.send({
				code: 200,
				msg: 'update suc'
			});
		} else {
			res.send({
				code: 301,
				msg: 'update err'
			});
		}
	});
});

//5.删除用户
router.delete('/:xid', (req, res, next) => {
	//获取数据
	var obj = req.params;
	console.log(obj);
	var $xid = obj.xid;
	//验证编号是否为空
	if (!$xid) {
		res.send({
			code: 401,
			msg: 'xid required'
		});
		return;
	}
	//执行SQL语句，删除对应的数据
	pool.query('DELETE FROM xh_user WHERE xid=?', [$xid], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		//判断是否删除成功
		if (result.affectedRows > 0) {
			res.send({
				code: 200,
				msg: 'delete suc'
			});
		} else {
			res.send({
				code: 301,
				msg: 'delete err'
			});
		}
	});
});

//6.检测邮箱是否注册
router.get('/checkemail', (req, res, next) => {
	var obj = req.query;
	var $email = obj.email;
	if (!$email) {
		res.send({
			code: 401,
			msg: "email required"
		});
		return;
	}
	pool.query('SELECT xid FROM xh_user WHERE email=? LIMIT 1', $email, (err, result) => {
		if (err) {
			next(err);
			return;
		};
		if (result.length > 0) {
			res.send({
				code: 201,
				msg: 'exists'
			});
		} else {
			res.send({
				code: 200,
				msg: 'non-exists'
			});
		}
	})
});

//7.检测电话是否注册
router.get('/checkphone', (req, res) => {
	var obj = req.query;
	var $phone = obj.phone;
	if (!$phone) {
		res.send({
			code: 401,
			msg: "phone required"
		});
		return;
	}
	pool.query('SELECT xid FROM xh_user WHERE phone=? LIMIT 1', $phone, (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({
				code: 201,
				msg: 'exists'
			});
		} else {
			res.send({
				code: 200,
				msg: 'non-exists'
			});
		}
	})
});

//9.检测用户名是否注册
router.get('/checkuname', (req, res, next) => {
	var obj = req.query;
	var $uname = obj.uname;
	if (!$uname) {
		res.send({
			code: 401,
			msg: "uname required"
		});
		return;
	}
	pool.query('SELECT xid FROM xh_user WHERE uname=? LIMIT 1', $uname, (err, result) => {
		if (err) {
			next(err);
			return;
		}
		if (result.length > 0) {
			res.send({
				code: 201,
				msg: 'exists'
			});
		} else {
			res.send({
				code: 200,
				msg: 'non-exists'
			});
		}
	})
});
//10.退出登录
router.get('/logout', (req, res) => {
	req.session.destroy();
	res.send({
		code: 200,
		msg: 'logout succ'
	});
});
//11.返回当前登录用户的信息
router.get('/sessiondata', (req, res) => {
	res.send({
		xid: req.session.loginUid,
		uname: req.session.loginUname
	});
});
//导出路由器
module.exports = router;
